package com.ohimy.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.ohimy.entity.Forum;
import com.ohimy.entity.ForumBody;
import com.ohimy.entity.Group;
import com.ohimy.service.impl.ForumService;
import com.ohimy.service.impl.GroupService;
import com.ohimy.service.impl.UserService;

@Controller("forumAction")
public class ForumAction extends BaseAction {

	private static final long serialVersionUID = -5807450070212662986L;

	@Resource(name = "forumService")
	private ForumService forumService;

	@Resource(name = "groupService")
	private GroupService groupService;

	@Resource(name = "userService")
	private UserService userService;

	private Forum forum = new Forum();
	private ForumBody forumBody = new ForumBody();
	private List<ForumBody> forumBodyList;
	private Group group = new Group();
	private String page_index, gid, maxResult; // 当前页，接受的ID，总数
	private long total;
	private List<Forum> groupForumList;

	@Override
	public String execute() throws Exception {
		group = groupService.getGroupById(id);
		total = forumService.getForumByTotal(id);
		result = "/WEB-INF/site/forum/index.jsp";
		return "ohimy";
	}

	/**
	 * 新发表一张帖子
	 * 
	 * @return
	 * @throws Exception
	 */
	public String create() throws Exception {
		group = groupService.getGroupById(id);
		result = "/WEB-INF/site/forum/new.jsp";
		return "ohimy";
	}

	/**
	 * 帖子的保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		forum.setGroup(group);
		forum.setCreated(new Date());
		forum.setPublisher(userService.findUserById(getSession().getId()));
		forum.setLastPublish(new Date());
		forumService.save(forum);
		return null;
	}

	/**
	 * 查看帖子内容
	 * 
	 * @return
	 * @throws Exception
	 */
	public String view() throws Exception {
		group = groupService.getGroupById(Integer.parseInt(this.gid));
		total = forumService.getForumBodyByTotal(id);
		// 每查看一篇帖子，更新查看数量++
		forum = forumService.updateViewNumber(id);
		result = "/WEB-INF/site/forum/view.jsp";
		return "ohimy";
	}

	/**
	 * 帖子的分页查看
	 * 
	 * @return
	 * @throws Exception
	 */
	public String show() throws Exception {
		int page_index = Integer.parseInt(this.page_index);
		// 处理数据库返回的数据
		forumBodyList = forumService.getForumBodyByPager(id, page_index * 10,
				10);
		result = "/WEB-INF/site/ajax/group_forum_apply.jsp";
		return "ohimy";
	}

	/**
	 * ajax远程调用的论坛列表 每页显示7个主题
	 * 
	 * @return
	 * @throws Exception
	 */
	public String page() throws Exception {
		long gid = Long.parseLong(this.gid);
		int page_index = Integer.parseInt(this.page_index);
		int maxResult = Integer.parseInt(this.maxResult);
		// 处理数据库返回的数据
		groupForumList = forumService.getForumByPager(gid, page_index
				* maxResult, maxResult);
		result = "/WEB-INF/site/ajax/group_forum.jsp";
		return "ohimy";
	}

	/**
	 * 发表一个回复
	 * 
	 * @return
	 * @throws Exception
	 */
	public String reply() throws Exception {
		forumBody.setApplier(userService.findUserById(getSession().getId()));
		forumBody.setForum(forumService.getForumById(id));
		forumBody.setCreated(new Date());
		// 保存返回了刚回复的实体，储存在List中
		forumBodyList = forumService.saveReply(forumBody);
		// 更新最后一次回复
		forum = forumService.getForumById(id);
		forum.setLastPublish(new Date());
		forumService.updateLastPublish(forum);
		result = "/WEB-INF/site/ajax/group_forum_apply.jsp";
		return "ohimy";
	}

	/* *********************************** */
	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}

	public String getPage_index() {
		return page_index;
	}

	public void setPage_index(String page_index) {
		this.page_index = page_index;
	}

	public List<Forum> getGroupForumList() {
		return groupForumList;
	}

	public void setGroupForumList(List<Forum> groupForumList) {
		this.groupForumList = groupForumList;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}

	public Forum getForum() {
		return forum;
	}

	public void setForum(Forum forum) {
		this.forum = forum;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public List<ForumBody> getForumBodyList() {
		return forumBodyList;
	}

	public void setForumBodyList(List<ForumBody> forumBodyList) {
		this.forumBodyList = forumBodyList;
	}

	public ForumBody getForumBody() {
		return forumBody;
	}

	public void setForumBody(ForumBody forumBody) {
		this.forumBody = forumBody;
	}

	public String getMaxResult() {
		return maxResult;
	}

	public void setMaxResult(String maxResult) {
		this.maxResult = maxResult;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}